package org.evolAI.organism.util;

import org.evolAI.organism.domain.Code;
import org.evolAI.organism.domain.LongTermMemory;
import org.evolAI.organism.domain.Memory;
import org.evolAI.organism.domain.MemoryGroup;
import org.evolAI.organism.domain.Organism;
import org.evolAI.organism.domain.Step;
import org.evolAI.organism.service.OrganismService;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Schema {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		createSchema();
		saveOrg();
    }
	
	private static void createSchema() {
		Configuration cfg = new Configuration();
		cfg
		.addAnnotatedClass(Organism.class)
		.addAnnotatedClass(Code.class)
		.addAnnotatedClass(Step.class)
		.addAnnotatedClass(LongTermMemory.class)
		.addAnnotatedClass(MemoryGroup.class)
		.addAnnotatedClass(Memory.class)
		.setProperty(Environment.USER, "")
		.setProperty(Environment.PASS, "")
		.setProperty(Environment.URL, "")
		.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect")
		.setProperty(Environment.DRIVER, "");
		
        SchemaExport export = new SchemaExport(cfg);
        export.setOutputFile("schema.sql");
        export.setDelimiter(";");
        export.create(true, false);
	}
	
	private static void saveOrg() {
		
		String[] contexts = new String[] {"datasource-context.xml", "persistence-context.xml", "service-context.xml", "application-context.xml"};
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(contexts);
		OrganismService service = (OrganismService) ctx.getBean("organismService");
		service.addOrganism(Organism.defaultOrganism());
		
	}

}
